/// @file db_ao.h
/// @brief Database Active Object for the authentication system
///
/// This file defines the DbAo class which handles all database operations
/// and communicates with the AuthAo through events.

#ifndef DB_AO_H
#define DB_AO_H

#include "qpcpp.hpp"
#include "auth_events.h"
#include "user_repository.h"
#include "session_repository.h"
#include "database_manager.h"

/// @class DbAo
/// @brief Database Active Object
///
/// This class handles all database operations and communicates with the AuthAo
/// through events. It encapsulates all SQLite operations and provides a clean
/// event-based interface for database access.
class DbAo : public QP::QActive
{
private:
    DatabaseManager* db_manager_;      ///< Database manager instance
    UserRepository* user_repo_;        ///< User repository instance
    SessionRepository* session_repo_;  ///< Session repository instance

public:
    /// @brief Constructor
    /// @param db_path Path to the database file
    DbAo(const std::string& db_path);

    /// @brief Destructor
    ~DbAo();

protected:
    /// @brief Initial state handler
    Q_STATE_DECL(initial);

    /// @brief Processing state handler
    Q_STATE_DECL(processing);

private:
    // State handler methods
    static QP::QState initial(DbAo* const me, QP::QEvt const* const e);
    static QP::QState processing(DbAo* const me, QP::QEvt const* const e);

    // Helper methods for processing database operations
    void processUserFindByUsernameRequest(const DbUserFindByUsernameRequestEvt* const evt);
    void processUserCreateRequest(const DbUserCreateRequestEvt* const evt);
    void processSessionCreateRequest(const DbSessionCreateRequestEvt* const evt);
    void processSessionRemoveRequest(const DbSessionRemoveRequestEvt* const evt);
    void processSessionFindByUserIdRequest(const DbSessionFindByUserIdRequestEvt* const evt);

    // Helper methods for database initialization
    void initializeDatabase();
};

/// @brief Global instance of the DbAo
extern DbAo* const the_AO_Db;

#endif  // DB_AO_H